package stark.app.function.map;

import android.Manifest;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.List;

import androidx.annotation.NonNull;
import stark.app.R;
import stark.app.databinding.ActivityMapDemoBinding;
import stark.app.function.map.amap.AmapDemoActivity;
import stark.common.basic.base.BaseNoModelActivity;

/**
 * Author：daizhongbin on 2021/9/9
 * Email：1027154338@qq.com
 * Desc:
 */
public class MapDemoActivity extends BaseNoModelActivity<ActivityMapDemoBinding> {

    private static final int TYPE_NAVI = 1;
    private static final int TYPE_LOC = 2;

    @Override
    protected int onCreate() {
        return R.layout.activity_map_demo;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopLoc();
    }

    @Override
    protected void initView() {
        mDataBinding.btnAmap.setOnClickListener(v -> {
            handleClick(TYPE_NAVI);
        });
        mDataBinding.btnLoc.setOnClickListener(v -> {
            handleClick(TYPE_LOC);
        });
    }

    @Override
    protected void initData() {
    }

    private void handleClick(int type) {
        PermissionUtils.permission(Manifest.permission.ACCESS_FINE_LOCATION)
                .callback(new PermissionUtils.SingleCallback() {
                    @Override
                    public void callback(boolean isAllGranted, @NonNull List<String> granted, @NonNull List<String> deniedForever, @NonNull List<String> denied) {
                        if (!isAllGranted) {
                            ToastUtils.showLong("未授予权限");
                        }
                        switch (type) {
                            case TYPE_NAVI:
                                startActivity(AmapDemoActivity.class);
                                break;
                            case TYPE_LOC:
                                startLoc();
                                break;
                        }
                    }
                })
                .request();
    }

    private AMapLocationClient mLocClient;

    private void startLoc() {
        if (mLocClient != null) {
            return;
        }
        AMapLocationClient client = new AMapLocationClient(this);
        mLocClient = client;
        AMapLocationClientOption clientOption = new AMapLocationClientOption();
        clientOption.setInterval(5000);
        clientOption.setNeedAddress(true);
        clientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        client.setLocationOption(clientOption);
        client.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (aMapLocation == null) return;
                int errCode = aMapLocation.getErrorCode();
                if (errCode != 0) return;
                double lat = aMapLocation.getLatitude();
                double lon = aMapLocation.getLongitude();

                //aMapLocation.getAddress();//地址，如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。
                //aMapLocation.getCountry();//国家信息
                //aMapLocation.getProvince();//省信息
                String city = aMapLocation.getCity();//城市信息
                String district = aMapLocation.getDistrict();//城区信息
                //aMapLocation.getStreet();//街道信息
                //aMapLocation.getStreetNum();//街道门牌号信息
                //String cityCode = aMapLocation.getCityCode();//城市编码
                //aMapLocation.getAdCode();//地区编码
                StringBuilder builder = new StringBuilder();
                builder.append("定位信息：\n");
                builder.append("纬度：" + String.format("%.2f", lat) + ", 经度：" + String.format("%.2f", lon));
                builder.append("\n");
                builder.append(city + " " + district);
                mDataBinding.tvLocRet.setText(builder.toString());
            }
        });
        client.startLocation();
    }

    private void stopLoc() {
        if (mLocClient != null) {
            mLocClient.stopLocation();
            mLocClient.onDestroy();
            mLocClient = null;
        }
    }
}
